﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace CP2011_Assignment
{
    public partial class CreateProductUI : Form
    {
        ComboBox[] comboArr = new ComboBox[5];
        public CreateProductUI()
        {
            InitializeComponent();
            buildComboBox(new InventorySystem(), comboArr);
        }

       

        private void btnAddProd_Click(object sender, EventArgs e)
        {
            //check input type
            int x;
            bool intCheck1 = Int32.TryParse(textBoxQty.Text, out x);
            bool intCheck2 = Int32.TryParse(txtBoxQtyNeeded1.Text, out x);
            bool intCheck3 = Int32.TryParse(txtBoxQtyNeeded2.Text, out x);
            bool intCheck4 = Int32.TryParse(txtBoxQtyNeeded3.Text, out x);
            bool intCheck5 = Int32.TryParse(txtBoxQtyNeeded4.Text, out x);
            bool intCheck6 = Int32.TryParse(txtBoxQtyNeeded5.Text, out x);

            //check if there are any empty field
            bool inputCheck1 = string.IsNullOrEmpty(textBoxName.Text);
            bool inputCheck2 = string.IsNullOrEmpty(textBoxDesc.Text);
            bool inputCheck3 = string.IsNullOrEmpty(textBoxQty.Text);
            
            if (inputCheck1 == false && inputCheck2 == false && inputCheck2 == false && inputCheck3 == false)
            {
                if (intCheck1 == true && intCheck2 == true && intCheck3 == true && intCheck4 == true && intCheck5 == true && intCheck6 == true)
                {
                    try
                    {

                        Product prod = new Product(textBoxName.Text, textBoxDesc.Text, Convert.ToInt32(textBoxQty.Text));
                        addRecord(new ProductionSystem(), prod);

                        TempStorage ts = new TempStorage();

                        ComboBox[] comboArr = new ComboBox[5];
                        comboArr[0] = comboInvBox1;
                        comboArr[1] = comboInvBox2;
                        comboArr[2] = comboInvBox3;
                        comboArr[3] = comboInvBox4;
                        comboArr[4] = comboInvBox5;

                        TextBox[] textBoxArr = new TextBox[5];
                        textBoxArr[0] = txtBoxQtyNeeded1;
                        textBoxArr[1] = txtBoxQtyNeeded2;
                        textBoxArr[2] = txtBoxQtyNeeded3;
                        textBoxArr[3] = txtBoxQtyNeeded4;
                        textBoxArr[4] = txtBoxQtyNeeded5;

                        //loop through the array
                        for (int i = 0; i < 4; i++)
                        {
                            //check whether the input is empty or not
                            if (comboArr[i].Text.Length != 0 && textBoxArr[i].Text.Length != 0)
                            {
                                ts.setString1(comboArr[i].Text);
                                ts.setValue4(Convert.ToDouble(textBoxArr[i].Text));
                                ts.setValue3((Convert.ToDouble(textBoxArr[i].Text) * Convert.ToDouble(textBoxQty.Text)));
                                addRecipe(new ProductionSystem(), ts, prod.getName());
                            }
                        }
                        
                        MessageBox.Show("New Product Successfuly Created!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Hide();
                        this.Owner.Show();
                        this.Dispose();
                    }
                    catch
                    {
                        MessageBox.Show("Failed to Execute Query. Have you enter the Inventory Needed and its amount?", "Failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }
                }// end integer checking if

                else
                {
                    
                    MessageBox.Show("Input format is incorrect, please insert only integer value QUANTITY PER DAY and QUANTITY NEEDED", "Incorrect Input Format", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    this.Show();
                }
            }//end empty field checking

            else
            {
                MessageBox.Show("Some fields are empty!", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                this.Show();
            }
        }

        //specific methods of production system
        private void addRecipe(IDataController prodSystem, TempStorage tempStrge, String prodName)
        {
            ((ProductionSystem)prodSystem).addRecipe(tempStrge, prodName);

        }


        private void addRecord(IDataController dataCtrl, Product prod)
        {
              //create new product object 
            
            dataCtrl.addToDatabase(prod);
        }

        
        private void buildComboBox(IDataController dataCtrl, ComboBox[] comboArr)
        {
            ArrayList values = dataCtrl.getRecord("SELECT Inv_Name FROM Inventory");
            comboArr[0] = comboInvBox1;
            comboArr[1] = comboInvBox2;
            comboArr[2] = comboInvBox3;
            comboArr[3] = comboInvBox4;
            comboArr[4] = comboInvBox5;

            for (int i = 0; i < comboArr.Length; i++)
            {
                comboArr[i].Items.AddRange(values.ToArray());

            }

        }
     
    }
}
